package com.gxa.autoparts.admin.controller;

import com.github.pagehelper.PageInfo;
import com.gxa.autoparts.admin.service.CouponService;
import com.gxa.autoparts.core.common.JsonResult;
import com.gxa.autoparts.core.pojo.Coupon;
import com.gxa.autoparts.core.pojo.CouponUseRecord;
import com.gxa.autoparts.core.pojo.CouponUseRecordVo;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author Bei
 * @date 2022/1/11 9:11
 */
@RestController
@RequestMapping("/coupon")
public class CouponController {

    @Autowired
    CouponService couponService;

    @PostMapping("/couponSelect")
    @ApiOperation("优惠券全查询接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "页码",required = true,dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(name = "size",value = "每页记录数",required = true,dataType = "Integer",paramType = "query")
    })
    public Map couponSelect(Integer page,Integer size) throws ParseException, IllegalAccessException {
        List<Map> maps = couponService.couponSelectAll(page, size);
        for (Map map : maps) {
            Object collectStartTime = map.get("collectStartTime");
            Object collectEndTime = map.get("collectEndTime");
            StringBuffer stringBuffer = new StringBuffer();
            map.put("startAndEndTime",stringBuffer.append(collectStartTime).append("~").append(collectEndTime));
        }
        List<Coupon> coupons = couponService.selectAll();
        int size1 = coupons.size();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(maps);
        jsonResult.put("count",size1);
        return jsonResult.getDataMap();
    }

    @PostMapping("/couponSelectByLike")
    @ApiOperation("优惠券模糊查询接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name",value = "优惠券名称",required = false,dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "type",value = "优惠券类型",required = false,dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(name = "startTime",value = "起始时间",required = false,dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "endTime",value = "终止时间",required = false,dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "page",value = "页码",required = true,dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(name = "size",value = "每页记录数",required = true,dataType = "Integer",paramType = "query")
    })
    public Map couponSelectByLike(String name,Integer type,String startTime, String endTime,Integer page,Integer size) throws ParseException, IllegalAccessException {
        List<Map> maps = couponService.couponSelectByLike(name, type, startTime, endTime, page, size);
        for (Map map : maps) {
            Object collectStartTime = map.get("collectStartTime");
            Object collectEndTime = map.get("collectEndTime");
            StringBuffer stringBuffer = new StringBuffer();
            map.put("startAndEndTime",stringBuffer.append(collectStartTime).append("~").append(collectEndTime));
        }


        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(maps);

        return jsonResult.getDataMap();
    }


    @PostMapping("/couponInsert")
    @ApiOperation("优惠券添加接口(name的格式必须为： 满（）减（) 的格式")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "state",value = "优惠券状态",required = true,dataType = "Coupon",paramType = "body"),
//            @ApiImplicitParam(name = "name",value = "优惠券名称",required = true,dataType = "Coupon",paramType = "body"),
//            @ApiImplicitParam(name = "type",value = "优惠券类型",required = true,dataType = "Coupon",paramType = "body"),
//            @ApiImplicitParam(name = "remark",value = "优惠券描述",required = true,dataType = "Coupon",paramType = "body"),
//            @ApiImplicitParam(name = "collectStartTime",value = "可领取开始时间",required = false,dataType = "Coupon",paramType = "body"),
//            @ApiImplicitParam(name = "collectEndTime",value = "可领取结束时间",required = false,dataType = "Coupon",paramType = "body"),
//            @ApiImplicitParam(name = "useStartTime",value = "可使用开始时间",required = false,dataType = "Coupon",paramType = "body"),
//            @ApiImplicitParam(name = "useEndTime",value = "可使用结束时间",required = false,dataType = "Coupon",paramType = "body"),
//            @ApiImplicitParam(name = "useEffectiveDays",value = "领取后有效天数",required = true,dataType = "Coupon",paramType = "body"),
//            @ApiImplicitParam(name = "useEffectiveDays",value = "领取后有效天数",required = true,dataType = "Coupon",paramType = "body"),
//            @ApiImplicitParam(name = "count",value = "优惠券数量",required = true,dataType = "Coupon",paramType = "body"),
//            @ApiImplicitParam(name = "isExchange",value = "是否允许积分兑换",required = true,dataType = "Coupon",paramType = "body"),
//            @ApiImplicitParam(name = "useRule",value = "优惠券规则",required = true,dataType = "Coupon",paramType = "body")
//
//    })
    public Map couponInsert(@RequestBody  Coupon coupon){
        Coupon insert = couponService.insert(coupon);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        return jsonResult.getDataMap();
    }

    @PostMapping("/couponUpdate")
    @ApiOperation("优惠券编辑接口(name的格式必须为： 满（）减（) 的格式")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "id",value = "优惠券id",required = true,dataType = "Coupon",paramType = "body"),
//            @ApiImplicitParam(name = "state",value = "优惠券状态",required = true,dataType = "Coupon",paramType = "body"),
//            @ApiImplicitParam(name = "name",value = "优惠券名称",required = true,dataType = "Coupon",paramType = "body"),
//            @ApiImplicitParam(name = "type",value = "优惠券类型",required = true,dataType = "Coupon",paramType = "body"),
//            @ApiImplicitParam(name = "remark",value = "优惠券描述",required = true,dataType = "Coupon",paramType = "body"),
//            @ApiImplicitParam(name = "collectStartTime",value = "可领取开始时间",required = false,dataType = "Coupon",paramType = "body"),
//            @ApiImplicitParam(name = "collectEndTime",value = "可领取结束时间",required = false,dataType = "Coupon",paramType = "body"),
//            @ApiImplicitParam(name = "useStartTime",value = "可使用开始时间",required = false,dataType = "Coupon",paramType = "body"),
//            @ApiImplicitParam(name = "useEndTime",value = "可使用结束时间",required = false,dataType = "Coupon",paramType = "body"),
//            @ApiImplicitParam(name = "useEffectiveDays",value = "领取后有效天数",required = true,dataType = "Coupon",paramType = "body"),
//            @ApiImplicitParam(name = "useEffectiveDays",value = "领取后有效天数",required = true,dataType = "Coupon",paramType = "body"),
//            @ApiImplicitParam(name = "count",value = "优惠券数量",required = true,dataType = "Coupon",paramType = "body"),
//            @ApiImplicitParam(name = "isExchange",value = "是否允许积分兑换",required = true,dataType = "Coupon",paramType = "body"),
//            @ApiImplicitParam(name = "useRule",value = "优惠券规则",required = true,dataType = "Coupon",paramType = "body")
//
//    })
    public Map couponUpdate(@RequestBody Coupon coupon) {
        Integer integer = couponService.updateById(coupon);

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        return jsonResult.getDataMap();
    }

    @PostMapping("/deleteById")
    @ApiOperation("删除优惠券接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "优惠券id",required = true,dataType = "long",paramType = "query"),
    })
    public Map deleteById(Long id) {
        Integer integer = couponService.deleteById(id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        return jsonResult.getDataMap();
    }

    @PostMapping("/couponUpdateState")
    @ApiOperation("优惠券修改状态接口(name的格式必须为： 满（）减（) 的格式")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "优惠券id",required = true,dataType = "long",paramType = "query"),
    })
    public Map couponUpdateState(Long id) {
        Coupon coupon = couponService.selectById(id);
        Boolean state = coupon.getState();
        state = !state;
        coupon.setState(state);
        Integer integer = couponService.updateById(coupon);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        return jsonResult.getDataMap();
    }

    @PostMapping("/couponUseRecord")
    @ApiOperation("优惠券使用记录全查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "优惠券id",required = true,dataType = "long",paramType = "query"),
            @ApiImplicitParam(name = "page",value = "页码",required = true,dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(name = "size",value = "每页记录数",required = true,dataType = "Integer",paramType = "query")
    })
    public Map couponUseRecord(Long id,Integer page,Integer size) {
        List<Map> maps = couponService.couponUseRecord(id,page,size);
        Integer integer = couponService.couponUseRecordCount(id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(maps);
        jsonResult.put("count",integer);
        return jsonResult.getDataMap();
    }

    @PostMapping("/couponUseRecordByLike")
    @ApiOperation("优惠券使用记录模糊查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "优惠券id",required = false,dataType = "Long",paramType = "query"),
            @ApiImplicitParam(name = "name",value = "优惠券名称",required = false,dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "type",value = "优惠券类型",required = false,dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(name = "userName",value = "会员昵称",required = false,dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "phone",value = "会员账号",required = false,dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "useStartTime",value = "可使用开始时间",required = false,dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "useEndTime",value = "可使用结束时间",required = false,dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "page",value = "页码",required = true,dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(name = "size",value = "每页记录数",required = true,dataType = "Integer",paramType = "query")
    })
    public Map couponUseRecordByLike(Long id,String name,Integer type,String userName,String phone,String useStartTime,String useEndTime,Integer page,Integer size) {
        List<Map> maps = couponService.couponUseRecordByLike(id, name, type, userName, phone, useStartTime, useEndTime, page, size);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(maps);
        return jsonResult.getDataMap();
    }


    @PostMapping("/selectType")
    @ApiOperation("查询优惠券类别")
    public Map selectType(){
        List<Integer> list = couponService.selectType();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(list);
        return jsonResult.getDataMap();
    }

    @PostMapping("/couponNoAndYes")
    @ApiOperation("查询优惠券已领和未领取的接口")
    public Map couponNoAndYes(Long id) {

        List<Map> no = couponService.couponNo(id);
        List<Map> yes = couponService.couponYes(id);

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.put("已领",yes);
        jsonResult.put("未领",no);
        return jsonResult.getDataMap();
    }

}
